<?php
    require_once '../Tool/Common/PrepareInput.php';

    session_start();
    if (empty($_SESSION["userType"])) {
        $url="../Index.php";
		include_once '../Tool/Common/Redirect.php';
		exit();
    }
    
    $NRIC=$CustomerName=$Citizenship=$BeforeDeviation=$AfterDeviation=$DateOfBirth=$Gender=$CentreId="";  

    if(isset($_REQUEST['NRIC'])){ 
        $NRIC=prepareInput($_REQUEST['NRIC']); 
    }
    if(isset($_REQUEST['CustomerName'])) { 
        $CustomerName=prepareInput($_REQUEST['CustomerName']); 
    }
    if(isset($_REQUEST['Citizenship'])) { 
        $Citizenship=prepareInput($_REQUEST['Citizenship']); 
    }
    if(isset($_REQUEST['BeforeDeviation'])) { 
        $BeforeDeviation=prepareInput($_REQUEST['BeforeDeviation']); 
    }
    if(isset($_REQUEST['AfterDeviation'])) { 
        $AfterDeviation=prepareInput($_REQUEST['AfterDeviation']); 
    }
    if(isset($_REQUEST['DateOfBirth'])) { 
        $DateOfBirth=prepareInput($_REQUEST['DateOfBirth']); 
    }
    if(isset($_REQUEST['Gender'])) { 
        $Gender=prepareInput($_REQUEST['Gender']); 
    }
    if(isset($_REQUEST['CentreId'])) { 
        $CentreId=prepareInput($_REQUEST['CentreId']); 
    }

    $result=array();

    $result['flagNRIC']=0;  
    $result['flagCustomerName']=0;
    $result['flagCitizenship']=0;
    $result['flagBeforeDeviation']=0;
    $result['flagAfterDeviation']=0;
    $result['flagWrongDate']=0;
    $result['flagDateOfBirth']=0;
    $result['flagGender']=0;
    $result['flagCentreId']=0;

    if(!empty($NRIC)&&preg_match("/^[ST][0-9]{7}[JZIHGFEDCBA]$/",$NRIC)||preg_match('/^[FG][0-9]{7}[XWUTRQPNMLK]$/', $NRIC)){
        $result['flagNRIC']=1;
    }
    if(preg_match("/^[0-9A-Za-z-'' ]*$/",$CustomerName)&&!empty($CustomerName)){
        $result['flagCustomerName']=1;
    }
    if(preg_match("/^[A-Za-z ]*$/",$Citizenship)&&!empty($Citizenship)){
        $result['flagCitizenship']=1;
    }
    if(preg_match("/^[0-9]*$/",$BeforeDeviation)&&!empty($BeforeDeviation)){
        $result['flagBeforeDeviation']=1;
    }
    if(preg_match("/^[0-9]*$/",$AfterDeviation)&&!empty($AfterDeviation)){  
        $result['flagAfterDeviation']=1;
    }
    if($DateOfBirth < date('Y-m-d')){
        $result['flagWrongDate']=1;
    }
    if(preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",$DateOfBirth)&&!empty($DateOfBirth)){
        $result['flagDateOfBirth']=1;
    }
    if(preg_match("/^[A-Za-z]*$/",$Gender)&&!empty($Gender)){
        $result['flagGender']=1;
    }
    if(preg_match("/^-?[0-9]{1,4}$/",$CentreId)&&!empty($CentreId)){
        $result['flagCentreId']=1;
    }

    echo json_encode($result);
?>